﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace ControlsTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DriveTreeInit();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string _fullPath;

        /// <summary>
        /// Create a list-tree
        /// </summary>

        private void DriveTreeInit()
        {
            string[] _driversArray = Directory.GetLogicalDrives();

            this.treeView1.BeginUpdate();
            this.treeView1.Nodes.Clear();

            foreach (string s in _driversArray)
            {
                TreeNode drive = new TreeNode(s, 0, 0);
                treeView1.Nodes.Add(drive);

                GetDirs(drive);
            }
        }


        private void GetDirs(TreeNode TN)
        {
            DirectoryInfo[] diArray;

            TN.Nodes.Clear();
            string _fullPath = TN.FullPath;

            DirectoryInfo _dI = new DirectoryInfo(_fullPath);

            try
            {
                diArray = _dI.GetDirectories();
            }
            catch
            {
                return;
            }

            foreach(DirectoryInfo dirInfo in diArray)
            {
                TreeNode dir = new TreeNode(dirInfo.Name, 1, 2);
                TN.Nodes.Add(dir);
            }

        }

        private void treeView_OnBeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            treeView1.BeginUpdate();

            foreach(TreeNode TN in e.Node.Nodes)
            {
                GetDirs(TN);
            }

            treeView1.EndUpdate();
        }

        private void treeView_OnAfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode _selctedNode = e.Node;
            _fullPath = _selctedNode.FullPath;

            DirectoryInfo di = new DirectoryInfo(_fullPath);
            FileInfo[] _fiArray;
            DirectoryInfo[] _diArray;

            try
            {
                _fiArray = di.GetFiles();
                _diArray = di.GetDirectories();
            }
            catch
            {
                return;
            }

            listView1.Items.Clear();

            foreach (DirectoryInfo dirInfo in _diArray)
            {
                ListViewItem _lvi = new ListViewItem(dirInfo.Name);
                _lvi.SubItems.Add("0");
                _lvi.SubItems.Add(dirInfo.LastWriteTime.ToString());
                _lvi.ImageIndex = 0;

                listView1.Items.Add(_lvi);
            }

            foreach (FileInfo fileinfo in _fiArray)
            {
                ListViewItem _lvi = new ListViewItem(fileinfo.Name);
                _lvi.SubItems.Add(fileinfo.Length.ToString());
                _lvi.SubItems.Add(fileinfo.LastWriteTime.ToString());

                string filenameExtansion = Path.GetExtension(fileinfo.Name).ToLower();

                switch(filenameExtansion)
                {
                    case ".com":
                        {
                            _lvi.ImageIndex = 2;
                            break;
                        }
                    case ".exe":
                        {
                            _lvi.ImageIndex = 2;
                            break;
                        }
                    case ".hlp":
                        {
                            _lvi.ImageIndex = 3;
                            break;
                        }
                    case ".txt":
                        {
                            _lvi.ImageIndex = 4;
                            break;
                        }
                    case ".doc":
                        {
                            _lvi.ImageIndex = 5;
                            break;
                        }
                    default :
                        {
                            _lvi.ImageIndex = 1;
                            break;
                        }
                }

                listView1.Items.Add(_lvi);
            }
        }

        private void listView_OnItemActivate(object sender, EventArgs e)
        {
            foreach(ListViewItem lvi in listView1.SelectedItems)
            {
                string ext = Path.GetExtension(lvi.Text).ToLower();

                if (ext == ".txt" || ext == ".htm" || ext == ".html")
                {
                    try
                    {
                        richTextBox1.LoadFile(Path.Combine(_fullPath, lvi.Text),
                            RichTextBoxStreamType.PlainText);
                    }
                    catch
                    {
                        return;
                    }
                }
                else if (ext == ".rtf")
                {
                    try
                    {
                        richTextBox1.LoadFile(Path.Combine(_fullPath, lvi.Text),
                                RichTextBoxStreamType.RichText);
                    }
                    catch
                    {
                        return;
                    }
                }
            }
        }

    }
}
